/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/
#ifndef CANBUSSERVODRIVER_H
#define CANBUSSERVODRIVER_H

#include <qmap.h>

#include "ecattype.h"

#include "canbusservodriverconfig.h"
#include "canbusdevicedriver.h"
//class CanbusServoDriverConfig;
//class CanbusDeviceDriver;

#define D_SDO_WAIT_INTERVAL_TIME 500

/**
 * @brief The CanbusServoDriver class can伺服接口
 */
class CanbusServoDriver
{
public:
    CanbusServoDriver(QString configFileIn,int aliasIn, int busNumberIn, int deviceIdIn,
                      CanbusDeviceDriver *canbusDriverIn);
    ~CanbusServoDriver();

public:
    int getBatteryInfo( BatteryInformation &batteryInfoOut);
    int process_can_buffer();//处理ｃａｎ接收的数据帧，提取自己的数据
    int process_can_buffer_pdo();
    int sendSdoGetCommandFrame();

    int sendPdoData( unsigned short controlWord,int	targetPosition,int targetVelocity,
                     short targetTorque,double pulsePerRound1);
    int sendPdoData_twoMotor( unsigned short controlWord1,int	targetPosition1,int targetVelocity1,short targetTorque1,
                              double pulsePerRound1,
                               unsigned short controlWord2,int	targetPosition2,int targetVelocity2,short targetTorque2,
                              double pulsePerRound2);

    int sendNodeWatchDogFrame();
    int startPdoMode();
    int setNodeSafePeriodTime(int timeIn);
    int servOnDriver();//not used
    int servOffDriver();//not used
    int quickStopDriver();//not used

    int setDriverMode(E_MOTOR_CONTROL_MODE modeIn);
<<<<<<< HEAD
    int setDriverNodeProtectTime(int msTime);
=======
>>>>>>> origin/master
    int setPosition(int positionIn,bool isWaitFeedback);
    int setPwm(int pwmIn);
    int setSpeed(int speedIn,bool isWaitFeedback);
    int setMaxCurrency(double ample);
    int setRatedCurrency(double ample);
    int changeDriverModbusId(int modbusId_new);

    int getErrorCode( int& errorCodeReturn);
    int getPositionFeedback(int& positionFeedbackReturn);
    int setControlWord(int controlWordIn,bool isWaitFeedback);
    int getStatusWord(int& statusWordReturn);
    int getSpeedFeedback(double &speedFeedbackReturn);
    int getCurrencyFeedback(double &currencyFeedbackReturn);

<<<<<<< HEAD
    int getErrorCode_buffer(unsigned int &errorCodeReturn);
=======
    int getErrorCode_buffer( int& errorCodeReturn);
>>>>>>> origin/master
    int getPositionFeedback_buffer(int& positionFeedbackReturn);
    int getStatusWord_buffer(int& statusWordReturn);
    int getSpeedFeedback_buffer(double &speedFeedbackReturn);
    int getCurrencyFeedback_buffer(double &currencyFeedbackReturn);

    int getErrorCode_buffer_channel2(unsigned int &errorCodeReturn);
    int getPositionFeedback_buffer_channel2(int& positionFeedbackReturn);
    int getStatusWord_buffer_channel2(int& statusWordReturn);
    int getSpeedFeedback_buffer_channel2(double &speedFeedbackReturn);
    int getCurrencyFeedback_buffer_channel2(double &currencyFeedbackReturn);

    int initialDriverParameter();
//    int connectPort();
    bool isInitialOkStatus();
//    int checkModbusMaster();
    int getVendorId();
    int downloadServoSdoConfig();

protected:
    int initial(QString fileName);
private:
    int process_can_buffer_pdo_curtis();
    void realTimeSleep(long long umicoroSecond);
<<<<<<< HEAD
    int process_getErrorCode_buffer(unsigned int &errorCodeReturn);
=======
    int process_getErrorCode_buffer(int& errorCodeReturn);
>>>>>>> origin/master
    int process_getPositionFeedback_buffer(int& positionFeedbackReturn);
    int process_getStatusWord_buffer(int& statusWordReturn);
    int process_getSpeedFeedback_buffer(double &speedFeedbackReturn);
    int process_getCurrencyFeedback_buffer(double &currencyFeedbackReturn);

    int send_getErrorCode_command();
    int send_getPositionFeedback_command();
    int send_getStatusWord_command();
    int send_getSpeedFeedback_command();
    int send_getCurrencyFeedback_command();

private:
    bool isUpdateSucess;

<<<<<<< HEAD
    unsigned int errorCode;
=======
    int errorCode;
>>>>>>> origin/master
    int positionFeedback;
    int statusWord;
    double speedFeedback;
    double currencyFeedback;

    unsigned int errorCode_channel2;//通道２
    int positionFeedback_channel2;//通道２
    int statusWord_channel2;//通道２
    double speedFeedback_channel2;//通道２
    double currencyFeedback_channel2;//通道２

    double pulsePerRound_channel1;
    double pulsePerRound_channel2;


    int batteryVoltage;
    double batteryVolume;
    unsigned char controlWorld_xinsong;
    unsigned char motor1ZeroStatus;//1号电机找零状态
    unsigned char motor2ZeroStatus;//2号电机找零状态


private:


    CanbusServoDriverConfig* driverConfig;
    int alias;
    int busNumber;
    int deviceId;
    bool isInitialedOk;

    RegConfig regConfig;
    RegAddressInfo regAdd;
    E_VENDOR_ID vendorId;
    QVector<SdoConfig> sdoConfigList;

    E_MOTOR_CMD controlWord;
    int resultKey;
    E_MOTOR_CONTROL_MODE controlMode;
    CanbusDeviceDriver *canbusDriver;


};

#endif // CANBUSSERVODRIVER_H
